<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let tree = {
        val: 1,
        left: {
          val: 2,
          left: {
            val: 4
          },
          right: {
            val: 5
          }
        },
        right: {
          val: 3,
          left: {
            val: 6
          },
          right: {
            val: 7
          }
        }
      }
      var binaryTreePaths = function (root) {
        const result = []
        function preOrder(cur, path) {
          //终止条件 当前节点为子节点
          if (!cur.left && !cur.right) {
            return result.push([...path])
          }
          //确定集合范围。因为是二叉树，所以要保证递归的节点存在
          if (cur.left) {
            path.push(cur.left.val)
            preOrder(cur.left, path)
            path.pop()
          }
          if (cur.right) {
            path.push(cur.right.val)
            preOrder(cur.right, path)
            path.pop()
          }
        }
        preOrder(root, [root.val])
        return result
      }
      console.log(binaryTreePaths(tree))
    </script>
  </body>
</html>
